﻿using EntityModels.E03.Photographs;
using IRepositories;
using Microsoft.EntityFrameworkCore;
using ORM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Repositories
{
    public class PhotographRepository:BaseRepository<Guid,Photograph>,IPhotographRepository
    {
        
        public PhotographRepository(PSPDbContext context) : base(context)
        {
        }

        public async Task<List<Photograph>> GetPhotographsByFollow(Guid userId)
        {
            var followerIds = await Context.Follows.Where(x => x.AppUserId == userId).Select(x => x.FollowerId).ToListAsync();

            var photographs = await Context.Photographs
                .Include(x => x.AppUser)
                .Include(x => x.PhotographType)
                .Where(x => followerIds.Contains(x.AppUserId))
                .ToListAsync();
            return photographs;
        }

        public async Task<bool> IsWorkLikedByUserAsync(Guid id, Guid userId)
        {
            return await Context.Likes.AnyAsync(x => x.PhotographId == id && x.AppUserId == userId);
        }

        public async Task<bool> IsWorkStarByUserAsync(Guid id, Guid userId)
        {
            return await Context.StarItems.AnyAsync(x => x.PhotographId == id && x.Star.AppUserId == userId);
        }
    }
}
